#!/bin/bash
#########################################################
#                                                       #
#            NextionDriver Service Handler              #
#                                                       #
# Written for Pi-Star (http://www.mw0mwz.co.uk/pi-star) #
#                                                       #
#            By Lieven De Samblanx (ON7LDS)             #
#                                                       #
#         based on work of Andy Taylor (MW0MWZ)         #
#                                                       #
#                     Version 1.0                       #
#                                                       #
#########################################################

# Service Config
DAEMON=NextionDriver
DAEMON_PATH=/usr/local/bin/
CONFIG=/etc/mmdvmhost
DAEMON_OPTS="-c $CONFIG -i"
PGREP=/usr/bin/pgrep
KILL=/bin/kill
SLEEP=/bin/sleep
USER=root
GROUP=mmdvm
LOGDIR=/var/log/pi-star

# Pre-flight checks...
test -x ${DAEMON_PATH}${DAEMON} || exit 0
test -r $CONFIG || exit 0

#We start very early in the bootprocess, so we
# verify the logging directory exists,
# if not create it and setup the ownership / permissions
if [ ! -d $LOGDIR ]; then
  mkdir -p $LOGDIR
  chown ${USER}:${GROUP} $LOGDIR
  chmod 775 $LOGDIR
fi

case "$1" in
	start)
		if [ "$(sed -n '/^\[General\]/,/^\[/p' /etc/mmdvmhost | grep "^Display" | awk -F '=' '{print $2}' | tr -d '[:space:]')" != "Nextion" ]; then
			echo -e "$DAEMON should not run"
			exit 0;
		fi
		if [ "$(sed -n '/^\[Nextion\]/,/^\[/p' /etc/mmdvmhost | grep "^Port" | awk -F '=' '{print $2}' | tr -d '[:space:]')" != "/dev/ttyNextionDriver" ]; then
			echo -e "$DAEMON should not run"
			exit 0;
		fi
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is already running as PID "`$PGREP $DAEMON`
			exit 0;
		else
			nice -n -10 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS}
			echo -e "$DAEMON started as PID "`$PGREP $DAEMON`
			exit 0;
		fi
		;;

	stop)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			$KILL `${PGREP} ${DAEMON}`
			if [ `${PGREP} ${DAEMON}` ]; then killall ${DAEMON}; fi
			exit 0;
		else
			echo -e "$DAEMON is not running"
			exit 0;
		fi
		;;

	restart)
		if [ `$PGREP $DAEMON` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			$KILL `${PGREP} ${DAEMON}`
			$SLEEP 3
			if [ "$(sed -n '/^\[General\]/,/^\[/p' /etc/mmdvmhost | grep "^Display" | awk -F '=' '{print $2}' | tr -d '[:space:]')" != "Nextion" ]; then
				echo -e "$DAEMON should not run"
				exit 0;
			fi
			if [ "$(sed -n '/^\[Nextion\]/,/^\[/p' /etc/mmdvmhost | grep "^Port" | awk -F '=' '{print $2}' | tr -d '[:space:]')" != "/dev/ttyNextionDriver" ]; then
				echo -e "$DAEMON should not run"
				exit 0;
			fi
			nice -n -10 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS}
			echo -e "$DAEMON re-started as PID "`${PGREP} ${DAEMON}`
			exit 0;
		else
			echo -e "$DAEMON is not running"
			nice -n -10 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS}
			echo -e "$DAEMON started as PID "`${PGREP} ${DAEMON}`
			exit 0;
		fi
		;;

	status)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is running as PID "`${PGREP} ${DAEMON}`
		else
			echo -e "$DAEMON is not running"
		fi
		;;

	*)
		echo $"Usage: $0 {start|stop|restart|status}"
		exit 0
esac
